package com.wk1un2.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wk1un2.entity.Administrator;
import com.wk1un2.service.AdministratorService;

/**
 * 管理员控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class AdministratorController {
	@Autowired // 注入
	AdministratorService service;

	@RequestMapping("/Backstage/adminlogin") // 登录的方法
	public String adminlogin(Administrator admin, Model model, HttpSession session) {
		System.out.println(456);
		// 获取用户名和密码
		String admin_name = admin.getAdmin_name();
		String admin_password = admin.getAdmin_password();

		Administrator a = service.findAllAdmin(admin_name, admin_password);

		if (a != null) {
			// 将用户对象添加到Session中
			session.setAttribute("USER_SESSION", a);
			// 重定向到主页面的跳转方法
			return "redirect:/Backstage/index.jsp";
		}
		// 如果账号密码不正确
		model.addAttribute("msg", "管理员名或密码错误，请重新登录！");
		return "forward:/Backstage/adminlogin.jsp";
	}

	
//	@RequestMapping("/addadmin")
//	@ResponseBody
//	public Object add(@RequestParam Map<String, Object> map) {
//		System.out.println(map.get("admin_name"));
//		System.out.println(map.get("admin_password"));
//		int i = service.add(map);
//		Result res = new Result(0, "", 0, i);
//		return res;
//	}
	
	@RequestMapping("/Backstage/index")
	public String toMain() {
		System.out.println(123);
		// 转发到WEB-INF/index.jsp页面
		return "/Backstage/index.jsp";
	}

	/**
	 * 安全退出
	 */
	@RequestMapping("/Backstage/logout")
	public String logout(HttpSession session) {
		// 清除session
		session.invalidate();
		// 重定向到登录页面的跳转方法
		return "forward:/Backstage/adminlogin.jsp";
	}
}
